package net.feixiang.creational.builder;

/**
 * 计算机类，使用 Builder 模式创建
 * <p>
 * 该类包含中央处理器、内存、硬盘和显卡等属性。使用 Builder 模式可以灵活地创建 Computer 对象，允许部分属性的设置。
 * </p>
 *
 * @author 飞翔
 * @see <a href="https://www.feixiang.net/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E7%AC%AC%E4%B8%80%E7%AB%A0%E3%80%81%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/14.html">建造者模式</a>
 */
public class Computer {
    private final String cpu;     // 中央处理器

    private final String memory;  // 内存大小

    private final String disk;    // 硬盘大小

    private final String graphicsCard;    // 显卡信息

    /**
     * 私有构造函数，使用Builder来创建Computer对象
     *
     * @param builder Builder对象
     */
    private Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.memory = builder.memory;
        this.disk = builder.disk;
        this.graphicsCard = builder.graphicsCard;
    }

    /**
     * 建造者类
     */
    public static class Builder {
        private String cpu = "";          // 默认值
        private String memory = "";       // 默认值
        private String disk = "";         // 默认值
        private String graphicsCard = ""; // 默认值

        public Builder() {
        }

        /**
         * 设置cpu
         *
         * @param cpu 中央处理器信息
         * @return Builder对象
         */
        public Builder cpu(String cpu) {
            this.cpu = cpu;
            return this;
        }

        /**
         * 设置memory
         *
         * @param memory 内存大小
         * @return Builder对象
         */
        public Builder memory(String memory) {
            this.memory = memory;
            return this;
        }

        /**
         * 设置disk
         *
         * @param disk 硬盘大小
         * @return Builder对象
         */
        public Builder disk(String disk) {
            this.disk = disk;
            return this;
        }

        /**
         * 设置graphicsCard
         *
         * @param graphicsCard 显卡信息
         * @return Builder对象
         */
        public Builder graphicsCard(String graphicsCard) {
            this.graphicsCard = graphicsCard;
            return this;
        }

        /**
         * 构建Computer对象
         *
         * @return Computer对象
         */
        public Computer build() {
            return new Computer(this);
        }
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", memory='" + memory + '\'' +
                ", disk='" + disk + '\'' +
                ", graphicsCard='" + graphicsCard + '\'' +
                '}';
    }
}